package com.ruoyi.applet.controller;

import javax.annotation.Resource;
import javax.xml.transform.Result;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.applet.domain.Goods;
import com.ruoyi.applet.domain.UserLogin;
import com.ruoyi.applet.domain.UsrBrowse;
import com.ruoyi.applet.service.IUsrBrowseService;
import com.ruoyi.applet.utli.*;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.domain.User;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.applet.service.IUserService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户信息Controller
 * 
 * @author ruoyi
 * @date 2024-11-12
 */
@CrossOrigin
@RestController
@RequestMapping("/applet")
public class UserController extends BaseController
{
    @Resource
    private IUserService userService;

    @GetMapping("/login")
    public Object Login(@RequestParam("usrPhone") String usrPhone , @RequestParam("usrPassword") String usrPassword){
        User user = userService.selectUsrPhoneANDPassword(usrPhone, usrPassword);
        return JwtUtil.generateToken(user);
    }

    @RequestMapping("/wxLogin")
    public Object wxLogin(@RequestParam("nikeName") String nikeName){
        User user = userService.selectByUsrName(nikeName);
        return JwtUtil.generateToken(user);
    }


    @GetMapping("/sendMessage")
    public AjaxResult sendMessage(@RequestParam("usrPhone") String usrPhone){
        //生成4位随机数
        String code = String.valueOf((int)
                ((Math.random() * 9 + 1) * Math.pow(10, 3)));
        //免费开发测试使用的模板ID为1，具体内容：【云通讯】您的验证码是{1}，请于{2}分钟内正确输入。其中{1}和{2}为短信模板参数。
        return success(RabbitMQUtils.sendCode(usrPhone, code));
    }

    @GetMapping("/register")
    public AjaxResult register(@RequestParam("usrName") String usrName,@RequestParam("usrPhone") String usrPhone,
                               @RequestParam("usrSex") String usrSex,@RequestParam("usrEmail") String usrEmail,
                               @RequestParam("usrPassword") String usrPassword){
        UserLogin user = new UserLogin();
        user.setUsrEmail(usrEmail);
        user.setUsrName(usrName);
        user.setUsrPassword(usrPassword);
        user.setUsrPhone(usrPhone);
        user.setUsrSex(usrSex);
        return toAjax(userService.insertAllBoolean(user));
    }

    @GetMapping("/usrName")
    public Boolean usrName(@RequestParam("usrName") String usrName){
        return userService.selectAllByUerNameInteger(usrName) == 0;
    }

    @GetMapping("/phone")
    public Boolean usrPhone(@RequestParam("usrPhone") String usrPhone){
        return userService.selectByUsrPhoneInteger(usrPhone) == 0;
    }

    @RequestMapping("/usrLike")
    public Integer usrLikeCount(@RequestParam("token") String token){
        return userService.selectAllByUsrIdLikeInteger(TokenByUsrId.tokenByUsrId(token));
    }

    @RequestMapping("/usrBrowse")
    public Integer usrBrowseCount(@RequestParam("token") String token){
        return userService.selectAllByUsrIdBrowseInteger(TokenByUsrId.tokenByUsrId(token));
    }

    @RequestMapping("/usrLikeList")
    public Object selectUsrLikeList(@RequestParam("token") String token){
        return userService.selectAllByUsrIdLikeList(TokenByUsrId.tokenByUsrId(token));
    }

    @RequestMapping("/usrBrowsesList")
    public Object selectUsrBrowseList(@RequestParam("token") String token){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("browseList",userService.selectAllByUsrIdBrowseList(TokenByUsrId.tokenByUsrId(token)));
        resultMap.put("timeList",userService.selectAllByBrowseTime(TokenByUsrId.tokenByUsrId(token)));
        return resultMap;
    }

    @RequestMapping("/usrDeleteLike")
    public Object deleteLikeGoods(@RequestParam("goodsId") Long goodsId){
        return userService.deleteByUsrIdBoolean(goodsId);
    }

    @Resource
    IUsrBrowseService usrBrowseService;

    @RequestMapping("/usrDeleteBrowse")
    public Object deleteBrowseGoods(@RequestParam("token") String token,@RequestParam("goodsId") Long goodsId){
        UsrBrowse usrBrowse = usrBrowseService.selectAllByBrowseGoodsIdUsrBrowse(TokenByUsrId.tokenByUsrId(token),goodsId);
        if(usrBrowse.getBrowseTimeId() != null){
            if(usrBrowseService.delByBrowseTimeId(usrBrowse.getBrowseTimeId()) != 0){
                return userService.deleteByGoodsIdBoolean(TokenByUsrId.tokenByUsrId(token),goodsId);
            }
        }
        return false;
    }

    @RequestMapping("/selectLikeGoods")
    public Object selectUsrLikeGoods(@RequestParam("token") String token,@RequestParam("goodsId") Long goodsId){
        return userService.selectAllByUsrIdAndGoodsId(TokenByUsrId.tokenByUsrId(token),goodsId);
    }

    @RequestMapping("/isLogin")
    public Object selectUsrLikeGoods(@RequestParam("token") String token){
        try{
            return TokenByUsrId.tokenByUsrId(token);
        }catch (NullPointerException e){
            return false;
        }
    }

    @RequestMapping("/insUserLike")
    public Object insertUsrGoodsId(@RequestParam("token") String token,@RequestParam("goodsId") Long goodsId){
        return userService.insertUserLikeGoods(TokenByUsrId.tokenByUsrId(token),goodsId);
    }

    @RequestMapping("/deleteBrowseAll")
    public Object deleteBrowseUserAll(@RequestParam("token") String token){
        return userService.deleteAll(TokenByUsrId.tokenByUsrId(token));
    }


}
